<?php
/*
 * Created on May 13, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

function pagination($base_url, $total_rows, $per_page , $uri_segment, $other = array()){
	$config['base_url'] = $base_url; // news/view/

	$config['total_rows'] = $total_rows;
	$config['per_page'] = $per_page;
	$config['uri_segment'] = $uri_segment; //4;
	$config['first_link'] = "<img src='".base_url()."images/admin/list-prev2.gif'>";
	$config['last_link'] = "<img src='".base_url()."images/admin/list-next2.gif'>";
	$config['next_link'] = "<img src='".base_url()."images/admin/list-next.gif'>";
	$config['prev_link'] = "<img src='".base_url()."images/admin/list-prev.gif'>";
	$config['num_tag_open'] = '<div class="page">';
	$config['num_tag_close'] = '</div>';
	$config['prev_tag_open'] = '<div class="page">';
	$config['prev_tag_close'] = '</div>';
	$config['first_tag_open'] = '<div class="page">';
	$config['first_tag_close'] = '</div>';
	$config['last_tag_open'] = '<div class="page">';
	$config['last_tag_close'] = '</div>';
	$config['next_tag_open'] = '<div class="page">';
	$config['next_tag_close'] = '</div>';
	$config['cur_tag_open'] = '<div class="page_red">';
	$config['cur_tag_close'] = '</div>';
	$config['full_tag_open'] = '<div style="text-align:center;">';
	$config['full_tag_close'] = '</div>';

	foreach($other as $k => $v){
		$config[$k] = $v;
	}

	$CI =& get_instance();

	$CI->pagination->initialize($config);
}

function pagination_front($base_url, $total_rows, $per_page , $uri_segment, $other = array()){
	$config['base_url'] = $base_url; // news/view/

	$config['total_rows'] = $total_rows;
	$config['per_page'] = $per_page;
	$config['uri_segment'] = $uri_segment; //4;
	$config['first_link'] = "<img src='".base_url()."images/admin/list-prev2.gif'>";
	$config['last_link'] = "<img src='".base_url()."images/admin/list-next2.gif'>";
	$config['next_link'] = "<img src='".base_url()."images/admin/list-next.gif'>";
	$config['prev_link'] = "<img src='".base_url()."images/admin/list-prev.gif'>";
	$config['num_tag_open'] = '<li class="page">';
	$config['num_tag_close'] = '</li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';
	$config['cur_tag_open'] = '<li class="current"><a href="#">';
	$config['cur_tag_close'] = '</a></li>';
	$config['full_tag_open'] = '<div class="pager"><ul class="page">';
	$config['full_tag_close'] = '</ul></div>';
	$config['anchor_class'] = 'class="page-number"';
	
	foreach($other as $k => $v){
		$config[$k] = $v;
	}

	$CI =& get_instance();

	$CI->pagination->initialize($config);
}

/*
 Lan vet ve danh muc cha:
 */
function getTrackNews($category_id=0, $categories = null, $lang = 'vi' ){
	if(!is_array($categories)){
		$CI =& get_instance();

		$query = "SELECT id_ncategory, id_parent_ncategory, ".$lang."_name AS name, link_rewrite FROM gtb_ncategory";
			
		$result = $CI->db->query($query);
			
		$categories = $result->result();
			
	}
	//print_r($categories);
	$result = array();

	for($i = 0; $i <count($categories); $i++){
		if($categories[$i]->id_ncategory == $category_id){
			$result[] = $categories[$i];
			$category_id = $categories[$i]->id_parent_ncategory;
			if($categories[$i]->id_parent_ncategory > 0)
			$result = array_merge(getTrackNews($category_id, $categories, $lang), $result);
			else return $result;
		}
	}

	return $result;
}

function getTrack($category_id=0, $categories = null, $lang = 'vi' ){
	if(!is_array($categories)){
		$CI =& get_instance();

		$query = "SELECT id_pcategory, id_parent_pcategory, ".$lang."_name AS name, link_rewrite, code FROM gtb_pcategory";
			
		$result = $CI->db->query($query);
			
		$categories = $result->result_array();
			
	}

	$result = array();

	for($i = 0; $i <count($categories); $i++){
		if($categories[$i]['id_pcategory'] == $category_id){
			$result[] = $categories[$i];
			$category_id = $categories[$i]['id_parent_pcategory'];
			if($categories[$i]['id_parent_pcategory'] > 0)
			$result = array_merge(getTrack($category_id, $categories, $lang), $result);
			else return $result;
		}
	}

	return $result;
}

/*
 * Lay tat ca cac node thuoc danh muc hien hanh
 * @access	public
 * @param	parent_id
 * @param	array category obeject
 * @return	array of id_pcategorys.
 */
function getElementsOfNode($parent, $categories = null){
	if(!is_array($categories)){
		$CI =& get_instance();

		$query = "SELECT id_pcategory, id_parent_pcategory FROM gtb_pcategory";
			
		$result = $CI->db->query($query);
			
		$tmp = $result->result();
			
		$categories = array();
		for($i = 0; $i <count($tmp); $i++){
			$category = $tmp[$i];
			//$tmp[$category->id_parent_pcategory][$category->id_pcategory] = $category;
			$categories[$category->id_parent_pcategory][$category->id_pcategory] = $category;
		}
	}

	$result = array();

	if(@isset($categories[$parent])){
		$num_record = count($categories[$parent]);
		foreach($categories[$parent] as $k => $v){
			$parent = $v->id_pcategory;
			$value = $v->id_pcategory;

			$result[] = $v->id_pcategory;

			$result = array_merge($result, getElementsOfNode($parent, $categories));

		}
	}

	return $result;
}

function loadLanguage(){
	$CI =& get_instance();

	$lang = $CI->session->userdata('lang');
	if(empty($lang))
	$lang = $CI->config->item('language');
	else
	$lang =  $CI->session->userdata('lang');

	$CI->lang->load('common', $lang);

}

function getLangCode(){
	$CI = & get_instance();
	$lang = $CI->session->userdata('lang');
	if(empty($lang))
	$lang = $CI->config->item('language');
	//else
	//	$lang =  $CI->session->userdata('lang');


	return $lang;
}


function fckEditor($name, $value = '', $width = '100%', $height = '300', $toolbarSet = 'Default', $config = null){
	$CI =& get_instance();

	if(!$config)
	$config = array(
			  'instanceName' => $name /*'content'*/,
			  'BasePath' => base_url().'system/plugins/fckeditor/',
			  'ToolbarSet' => $toolbarSet /*'Basic'*/ /*'Default'*/, 
			  'Width' => $width,
			  'Height' =>  $height,
			  'Value' => $value /*''*/
	);

	$CI->load->library('fckeditor', $config);

	return $CI->fckeditor;
}

function testGetFile(){
	$config = array(
 			'allowed' => array('JPEG', 'jpg', 'PNG', 'png', 'GIF', 'gif'),
 			'max_size'=> 1024000000 /*1Mb*/
	);

	$result = getFile($config, 'images/provider/logo', 'http://images8.dantri.com.vn/ThumbImages/Uploaded/2009/05/08/lut080520094_327.jpg');

	if(!$result['error']) echo 'Get file success.';
	else echo $result['message'];
}

function getFile($config, $destination_path, $address){
	$CI =& get_instance();

	$result = array();
	$result['error'] = false;
	$result['file_name'] = '';
	$result['message'] = '';

	if(!file_exists($destination_path)) {
		$result['error'] = true;
		$result['message'] .= $CI->lang->line('upload_no_filepath').'<br>';
		return $result;
	}

	$source = @fopen($address,"r");

	if(!$source) {
		$result['error'] = true;
		$result['message'] .= "The file not found at: <b>$address</b><br>";
		return $result;
	}

	$url_headers = get_headers ($address, 1);

	echo $mime_type = reset (explode (';', $url_headers['Content-Type']));
	$filesize= $url_headers['Content-Length'];
	$content_type = CONTENT_TYPE;
	eval($content_type);
	//print_r($content_type);
	if(!array_key_exists($mime_type, $content_type)){
		$result['error'] = true;
		$result['message'] .= $CI->lang->line('upload_invalid_filetype').'<br>';
		return $result;
	}


	$extend_name = $content_type[$mime_type];

	if(!in_array($extend_name, $config['allowed'])){
		$result['error'] = true;
		$result['message'] .= $CI->lang->line('upload_invalid_filetype').'<br>';
		return $result;
	}

	if($filesize > $config['max_size']){
		$result['error'] = true;
		$result['message'] .= $CI->lang->line('upload_invalid_filesize').'<br>';
		return $result;
	}

	$file_name = time().uniqid().'.'.$extend_name;
	$result['file_name'] = $file_name;

	$destination = fopen($destination_path.'/'.$file_name, 'w');

	$length=0;
	while (($a=fread($source,1024))&&($length<$filesize))
	{
		$length=$length+1024;
		fwrite($destination,$a);
	}
	fclose($source);
	fclose($destination);

	return $result;
}



/*
 Get directory base on date with format yyyy-mm-dd hh:i:ss.

 */
function getDirByDate($date, $path = ''){
	$tmp = explode(' ', $date);
	$tmp1 = $tmp[0];
	$tmp2 = $tmp[1];
	//$tmp = explode('-', $tmp);
	$tmp1 = str_replace('-', DIRECTORY_SEPARATOR, $tmp1);
	$tmp2 = str_replace(':', '', $tmp2);

	$dir = $path . $tmp1 . DIRECTORY_SEPARATOR . $tmp2;
	//$dir = $path . "2012";
	if (!is_dir($dir)) {
		if (mkdir($dir, 0777, true)) {
			return $dir;
		}
	}

	return $dir;
}


function sendMail($from, $to, $subject, $message){
	$CI =& get_instance();

	//$CI->load->library('fckeditor', $config);

	$config = Array(
  'protocol' => 'smtp',
  'smtp_host' => 'smtp.angelbeautycali.com',
	//'smtp_port' => 465,
  'smtp_user' => 'system@angelbeautycali.com', // change it to yours
  'smtp_pass' => 'System2012', // change it to yours
  'mailtype' => 'html',
  'charset' => 'utf-8',
  'wordwrap' => TRUE
	);

	$CI->load->library('email', $config);

	$CI->email->set_newline("\r\n");

	$CI->email->from($from); // change it to yours
	$CI->email->to($to); // change it to yours
	$CI->email->subject($subject);
	$CI->email->message($message);

	if($CI->email->send())
	{
		return true;
	}
	else
	{
		//show_error($this->email->print_debugger());
	}

	return false;
}

function getCountry($id){
	$lang = getLangCode();

	$CI =& get_instance();
	$field = $lang.'_name';
	$query = "SELECT $field  FROM gtb_country WHERE id = " . $id;
		
	$result = $CI->db->query($query);
		
	$tmp = $result->result_array();

	if (!empty($tmp))
		return $tmp[0][$field];


	return '';
}

?>
